﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Windows.Forms;
using System.Drawing;


namespace DragDropManager
{
    public interface IDropFileTarget
    {
        void FilesDropped(System.Drawing.Point p,System.Array a,string d);
    }

	public class DragDropManager
	{
        //private Form parent;
        private Control parent;

		//private DragEventArgs dragEventArgs;
        private delegate void DelegateOpenFiles(Point p,Array a,string d);
        private DelegateOpenFiles delegateOpenFiles;

//        public DragDropManager(Form form)
        public DragDropManager(Control parentControl)
		{
            parent = parentControl;

            if (!(parent is IDropFileTarget))
            {
                throw new Exception(
                    "DragDropManager needs IDropFileTarget interface");
            }

            delegateOpenFiles = new DelegateOpenFiles(((IDropFileTarget)parent).FilesDropped);

            parent.AllowDrop = true;

            parent.DragEnter += new System.Windows.Forms.DragEventHandler(this.OnDragEnter);
            parent.DragDrop += new System.Windows.Forms.DragEventHandler(this.OnDragDrop);
        }



        private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                if (e.Data.GetDataPresent(DataFormats.Text))
                {
                    e.Effect = DragDropEffects.Link;
                }
                else
                    e.Effect = DragDropEffects.None;
            }
        }

        private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            try
            {
				//dragEventArgs = e;
                Array a=null;// = (Array)e.Data.GetData(DataFormats.FileDrop);
                string d = "";

                if (e.Data.GetDataPresent(DataFormats.Text))
                {
                    a = new string[1];// Array[1];
                    a.SetValue(
                        e.Data.GetData(DataFormats.Text),
                        0);
                    d = DataFormats.Text;
                }
                else
                {
                    if (e.Data.GetDataPresent(DataFormats.FileDrop))
                    {
                        a = (Array)e.Data.GetData(DataFormats.FileDrop);
                        d = DataFormats.FileDrop;
                    }
                }

				Point p = new Point(e.X,e.Y);

                if ( a != null )
                {
					parent.BeginInvoke(delegateOpenFiles, new Object[] {p,a,d});
                    //parent.Activate();
                }
            }
            catch (Exception ex)
            {
            }
        }

	}
}
